//
//  BroadcastTableViewCell.m
//  xiaoyiNew
//
//  Created by TW on 2017/2/7.
//  Copyright © 2017年 讯一. All rights reserved.
//

#import "BroadcastTableViewCell.h"
#import "SubjectDetailsViewController.h"


@interface BroadcastTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *headImage;//头像
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//名字
@property (weak, nonatomic) IBOutlet UIView *textView;//文字内容
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;//内容
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeight;//内容高度
@property (weak, nonatomic) IBOutlet UIView *imagesView;//图片
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addrLabel;
@property (weak, nonatomic) IBOutlet UILabel *seeNumLabel;//浏览次数
@property (weak, nonatomic) IBOutlet UILabel *typeNameLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentTop;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imagesHeight;

@end
@implementation BroadcastTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    self.headImage.layer.cornerRadius = self.headImage.bounds.size.height/2;
    self.headImage.layer.masksToBounds = YES;
    //初始化标记
    for (int i = 0; i < 5; i ++) {
        UIImageView * image = [self viewWithTag:200 + i];
        
        image.layer.cornerRadius = image.bounds.size.height/2;
        image.layer.masksToBounds = YES;
    }
    // Initialization code
}


#pragma mark - set -

- (void)setModel:(BroadcastModel *)model {
    
    _model = model;
    self.nameLabel.text = model.name;
    //获取图片
    NSArray * images = [model.imageurl componentsSeparatedByString:@"|"];
    NSMutableArray * mArr = [NSMutableArray array];
    for (NSString * str in images) {
        if (str.length > 0) {
            [mArr addObject:str];
        }
    }
    [self changeImagesWithArr:mArr];
    if (NULLString([[NSString getBaseURL] stringByAppendingString:model.imageurl] )) {
       [self.headImage sd_setImageWithURL:[NSURL URLWithString:[@touxiangURL stringByAppendingString:model.portraiturl]] placeholderImage:[UIImage imageNamed:@"head"]];
    }
    self.contentLabel.text = model.conntents;
    self.timeLabel.text = [self dateTimeDifferenceWithStartTime:model.addtime];
    self.addrLabel.text = [NSString stringWithFormat:@"来自[%@]",model.housename];
    self.seeNumLabel.text = [NSString stringWithFormat:@"浏览%ld次",model.liulans];
    
    if ([model.type_name isEqualToString:@"#选择发布主题#"]) {
        
        self.typeNameLabel.text = @"";
        self.contentTop.constant = 0;
    }else {
    
        self.typeNameLabel.text = model.type_name;
    }
}


#pragma mark - action -
//设置图片
- (void)changeImagesWithArr:(NSArray *)arr {

    [self.imagesView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (int i = 0; i < arr.count; i ++) {
        UIImageView * image = [[UIImageView alloc] init];
        [image sd_setImageWithURL:[NSURL URLWithString:[NSString getImageWithSubURL:arr[i]]] placeholderImage:[UIImage imageNamed:@"head"]];
        [self.imagesView addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(10 + (110 + 5)*(i%3));
            make.top.mas_equalTo(5 + (110 + 5)*(i/3));
            make.size.mas_equalTo(CGSizeMake(110, 110));
        }];
    }
    NSInteger page;
    if (arr.count == 0) {
        page = 0;
    }else if (arr.count > 0 && arr.count <=3){
        page = 1;
    }else if (arr.count > 3 && arr.count <=6){
        
        page = 2;
    }else {
        
        page = 3;
    }
    self.imagesHeight.constant = 115 * page;
}
//设置时间
- (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime{
    
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    
    [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *startD =[date dateFromString:startTime];
    
    NSDate *endD = [NSDate date];
    
    NSTimeInterval start = [startD timeIntervalSince1970]*1;
    
    NSTimeInterval end = [endD timeIntervalSince1970]*1;
    
    NSTimeInterval value = end - start;
    
    int second = (int)value %60;//秒
    
    int minute = (int)value /60%60;
    
    int house = (int)value / (24 * 3600)%3600;
    
    int day = (int)value / (24 * 3600);
    
    NSString *str;
    
    if (day != 0) {
        
        str = [NSString stringWithFormat:@"%d天前",day];
        
    }else if (day==0 && house != 0) {
        
        str = [NSString stringWithFormat:@"%d小时前",house];
        
    }else if (day== 0 && house== 0 && minute!=0) {
        
        str = [NSString stringWithFormat:@"%d分钟前",minute];
        
    }else{
        
        str = [NSString stringWithFormat:@"%d秒前",second];
        
    }
    
    return str;
    
}
#pragma mark - 点击 -
//tag 300
- (IBAction)action_cellClick:(UIButton *)sender {
    
    NSInteger tag = sender.tag - 300;
    if (_delegate && [_delegate respondsToSelector:@selector(cellFuncClickWitnIndex:indexPath:)]) {
        [_delegate cellFuncClickWitnIndex:tag indexPath:self.indexPath];
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
